<?php

namespace api\modules\v1\models;
use Yii;
use api\common\models\Rate;
use api\common\models\Mar;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;
class Reportpc extends Report
{

    public function validateTypeUid($attribute, $params){
        if(!User::findOne(['id'=>$this->receive_uid])){
            $this->addError($this->receive_uid,'举报的用户不存在');
            return false;
        }
        $user = $this->getTokenUserId();
        if($user =='') {
            throw new NotFoundHttpException('access-token错误.');
        }
        if($this->receive_uid == $user['id']){
            $this->addError($this->receive_uid,'不能举报自己');
            return false;
        }
        return true;
    }
    /*
 * 举报提交
 * @return bool
 * @throws NotFoundHttpException
 */
    public function report(){
        if ($this->validate()) {
            $user = $this->getTokenUserId();
            if($user =='') {
                throw new NotFoundHttpException('access-token错误.');
            }

            if(self::findOne(['catid'=>$this->catid,'news_id'=>$this->news_id,'report_uid'=>$user['id'],'receive_uid'=>$this->receive_uid])){
                throw new NotFoundHttpException('请勿重复提交.');
            }
            $this->report_uid = $user['id'];
            $this->time = date('Y-m-d H:i:s');
            $this->report_num = 'JB'.date('YmdHis',time()).str_pad($user['id'],7, "0", STR_PAD_LEFT);
            if ($this->save(false)) {
                return true;
            }
        }
        return null;
    }
}
